/*****************************************************
	
	MathCommunicator Joe Pardue 8/16/08
	
******************************************************/
#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>

// include the special library for Smiley's Workshop 4
#include "smws4.h"

// define the parseCommand fuction
void parseCommand(char *, uint8_t);

int main(void) 
{ 
	char b = 0;
	char s[6];
	uint8_t count = 0;

	initialization(); // this is in libsmws4

	// the _P and PSTR weirdness allows you to store a string in flash
	// rather than wasting space in SRAM
	printf_P(PSTR("Math Communicator at your service!\n"));

	while(1)
	{
		while(b != '=')
		{
			b = (char)receiveByte(); // this is in libsmws4
			s[count++] = b;
		}
		parseCommand(s,count);
		b = 0;
		count = 0;
	}

    return 0;
}

void parseCommand(char *s,uint8_t cnt)
{
	int a,b;
	char t[3];

	t[0] = s[0];
	t[1] = s[1];
	t[2] = 0;
	a = atoi(t);	
	t[0] = s[3];
	t[1] = s[4];
	t[2] = 0;
	b = atoi(t);

	switch (s[2])
	{
		case '+':
			printf("%d + %d = %d\n",a,b,a+b);
			break;
		case '-':
			printf("%d - %d = %d\n",a,b,a-b);
			break;
		case '/':
			printf("%d / %d = %d\n",a,b,a/b);
			break;
		case '*':
			printf("%d * %d = %d\n",a,b,a*b);
			break;
		case '%':
			printf("%d %% %d = %d\n",a,b,a%b);
			break;
		default:
		{
			printf_P(PSTR("Say what?\n"));
			break;
		}
	}

}
